# Copyright (C) 2006  Apptility LLC  http://www.apptility.com
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


# Filters added to this controller will be run for all controllers in the application.
# Likewise, all the methods added will be available for all controllers.
# 
class DataaccessController < ApplicationController

  def save
  
   @role_id = params[:role_id]
   @data_access_actions= params[:data_access_action]
   @module_names= params[:module_name]
   
    # get list of modules 
    for @module_name in @module_names
      @data_access_actions.each { |@data_access_action| 
        @perm_id = "action_#{@module_name}_#{@data_access_action}"
        #AjubyLogger.logInfo "@perm_id #{@perm_id}"
        @perm_id_str = params[@perm_id]
        #AjubyLogger.logInfo "@perm_id_str #{@perm_id_str}"
        @action_daf_id_str = "action_#{@perm_id_str}"
        #AjubyLogger.logInfo "@action_daf_id_str #{@action_daf_id_str}"
        
        @action_daf_id = params[@action_daf_id_str]
        #AjubyLogger.logInfo "@action_daf_id #{@action_daf_id}"
        
        
        @modules_permission = Dataaccess.find(:first, :conditions => [ "role_id =? and permission_id=?", @role_id, @perm_id_str])       
        if @modules_permission==nil
          @modules_permission = Dataaccess.new
          @modules_permission.created_at = Time.now
          @modules_permission.created_by = current_user.id
        end
        update_modules_permission(@modules_permission, @role_id, @perm_id_str, @action_daf_id, @module_name, @data_access_action)
      }
    end
    redirect_to :controller=>'role', :action => 'list' , :id=>'#{@role_id}' , :savedop=>'1'
  end
  
  
    protected 
    def update_modules_permission(modules_permission,role_id, permission_id, action_daf_id, module_name, data_access_action)
        modules_permission.role_id = role_id
        modules_permission.permission_id = permission_id
        modules_permission.data_flag_id = action_daf_id
        modules_permission.updated_at = Time.now
        modules_permission.updated_by = current_user.id
        modules_permission.save
        if(action_daf_id=='4') # Collection
          create_update_collection(action_daf_id, role_id, module_name, data_access_action)
        else
          delete_stale_collections_if_any(action_daf_id, role_id, module_name, data_access_action)  
        end
        
    end
    
    def delete_stale_collections_if_any(action_daf_id, role_id, module_name, data_access_action)
        @collections = DataCollection.find_all_by_role_id_and_module_and_action(role_id, module_name, data_access_action)
        for @collection in @collections
          @collection.destroy
        end
    end
    
    def  create_update_collection(action_daf_id, role_id, module_name, data_access_action)
      
      request_str = "vals_collectionlist_#{module_name}_#{data_access_action}_selected_hidden"
      #AjubyLogger.logInfo "STR>>>>>>>>> #{request_str}"
      @collection_ids= params[request_str]
      
      #AjubyLogger.logInfo "@collection_ids>>>> #{@collection_ids}"
      
      # TODO , whole thing below needs to be reorganized for better performance.
      if @collection_ids!=nil
        # Split comma seperated values in array elements
        @collection_ids_array = @collection_ids.split(/,\s*/)
          for @collection_id in @collection_ids_array
            # TODO this needs to be improved, find by list of ids rather than one by one
            # bad for performance.
            @collection_id = @collection_id.strip
            if @collection_id!=""
              #AjubyLogger.logInfo "@collection_id>>> : #{@collection_id}"
              @data_collection = DataCollection.find_or_create_by_role_id_and_module_and_action_and_collection_id(role_id, module_name, data_access_action, @collection_id)
              update_data_collection(@data_collection)
              
            end
          end
          
          #AjubyLogger.logInfo "@collection_ids_array1>>>>>: #{@collection_ids_array}"
          # Now take care of deletes
          @data_collections_from_db = DataCollection.find_all_by_role_id_and_module_and_action(role_id, module_name, data_access_action)
          #AjubyLogger.logInfo "@collection_ids_array2>>>>>: #{@collection_ids_array}"
          if(@data_collections_from_db!=nil) 
            for @data_collection_from_db in @data_collections_from_db
              #AjubyLogger.logInfo "@data_collection_from_db>> #{@data_collection_from_db.collection_id.to_s} includes <==> #{@collection_ids_array.include?(@data_collection_from_db.collection_id.to_s)}"
              if (! @collection_ids_array.include?(@data_collection_from_db.collection_id.to_s))
                  @data_collection_from_db.destroy
              end
            end     
         end
         
         
      end  
    end
  
    def update_data_collection(data_collection)
        data_collection.updated_at = Time.now
        data_collection.updated_by = current_user.id
        data_collection.created_at = Time.now
        data_collection.created_by = current_user.id
        data_collection.organization_id = current_user.organization_id
        data_collection.save
    end 
  
end


